<template>
  <el-form
    ref="userForm"
    :rules="rules"
    :model="userInfo"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:" prop="username">
      <el-input v-model="userInfo.username" style="width:300px" />
    </el-form-item>
    <el-form-item label="密码:" prop="password">
      <el-input v-model="userInfo.password" type="password" style="width:300px" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="Hsubmit">更新</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { saveUserDetailById } from '@/api/employee'
import { getUserDetailById } from '@/api/user'
export default {
  data() {
    return {
      // userid:this.$route.query.id,//查询参数传参,写在路径问号后面
      userid: this.$route.params.id,
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '用户名不能为空', triggler: 'blur' }],
        password: [{ required: true, message: '密码不能为空', triggler: 'blur' }]
      }
    }
  },
  created() {
    this.loadUserDetailById()
  },
  methods: {
    // 更新触发函数,触发兜底校验
    Hsubmit() {
      this.$refs.userForm.validate(vaild => {
        if (!vaild) return
        this.loadsaveUserDetailById()
        this.$router.back()
      })
    },
    // 点击后,通过id查询该员工信息
    async loadUserDetailById() {
      try {
        const res = await getUserDetailById(this.userid)
        if (res.code !== 10000) return
        console.log('获取到某个员工的数据', res)
        this.userInfo = { ...res.data, password: '' }
        // 保存数据
      } catch (err) {
        console.log('获取员工错误的信息', err)
      }
    },
    // 保存员工信息函数
    async loadsaveUserDetailById() {
      try {
        const res = await saveUserDetailById(this.userInfo)
        console.log('saveUserDetailById', res)
        // 保存数据
      } catch (err) {
        console.log('saveUserDetailById', err)
      }
    }
  }
}
</script>

